/*
 * AisleNav Android Application
 * PickListActivity.java
 * project.AisleNav.ListActivity
 * 
 * CS495
 * 
 * Team:
 * 		Kirby Green
 * 		Mark Hale
 * 		Adam Warnock
 * 
 * Time: 6:14:34 PM
 * Date: Dec 4, 2011
 * 
 */
package project.AisleNav.ListActivity;

import java.util.Iterator;

import project.AisleNav.Backend.Item;
import project.AisleNav.NavigateActivity.NavigateStoreActivity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

// TODO: Auto-generated Javadoc
/**
 * PickListActivity.java Allows the user to pick a list of items
 * 
 * @author Kirby Green
 * @version 0.1
 */

public class PickListActivity extends ListActivity {
	
	/**
	 * Called when the activity is first created.
	 *
	 * @param savedInstanceState the saved instance state
	 * @author Kirby Green
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		project.AisleNav.AisleNavActivity.caller.load();
		
		//Add to ListView
		String[] names = project.AisleNav.AisleNavActivity.SharedLists.getFavoriteItems().getAllNames();
		for(int i = 0; i < names.length; ++i)
			System.out.println(names[i]);
		System.out.println("Done loading strings");

		setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, names));
		
		ListView lv = getListView();
		lv.setTextFilterEnabled(true);
		
		lv.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				
				project.AisleNav.AisleNavActivity.SharedLists.getCurSelectedItems()
					.setItems(project.AisleNav.AisleNavActivity.SharedLists.getFavoriteItems().getList((int)id).getItemsLinkedList());
				
				if(matchItems())
				{
					System.out.println("Items were matched correctly.");
					project.AisleNav.AisleNavActivity.caller.save();
					Intent i = new Intent(PickListActivity.this, NavigateStoreActivity.class);
					startActivity(i);
				}
				else
					finish();
			}
		});
	}

	/**
	 * Matches the user's currently selected list to the stores list, then
	 * places it in the navigable item list.
	 *
	 * @return if the items are successfully matched
	 */
	// TODO Check if done.
	private boolean matchItems() {
		System.out.println("Starting matchItems...");
		boolean found = false;
		
		project.AisleNav.AisleNavActivity.SharedLists
			.getNavigableStore().getItemsLinkedList().clear();
		project.AisleNav.AisleNavActivity.SharedLists
			.getNavigableStore().getStore().setName(
				project.AisleNav.AisleNavActivity.SharedLists
				.getCurrentStoreItems().getStore().getName());

		if (project.AisleNav.AisleNavActivity.SharedLists.getCurSelectedItems()
				.size() < 1) {
			System.out.println("CurSelectedItems is less than 1");
			return false;
		}
		if (project.AisleNav.AisleNavActivity.SharedLists
				.getCurrentStoreItems().getItems().size() < 1) {
			System.out.println("CurrentStoreItems is less than 1");
			return false;
		}
		Iterator<Item> usersItemsItr = project.AisleNav.AisleNavActivity.SharedLists
				.getCurSelectedItems().getItemsLinkedList().iterator();
		

		while (usersItemsItr.hasNext()) {
			Item userItem = usersItemsItr.next();
			String userItemName = userItem.getItemName();
			System.out.print("Trying to matching user[" + userItemName + "] ");
			
			Iterator<Item> storesItemsItr = project.AisleNav.AisleNavActivity.SharedLists
					.getCurrentStoreItems().getItemsLinkedList().iterator();

			while (storesItemsItr.hasNext()) {
				Item storeItem = storesItemsItr.next();
				String storeItemName = storeItem.getItemName();
				System.out.println("with store[" + storeItemName + "]");
				if (userItemName.equalsIgnoreCase(storeItemName)) {
					System.out.println("Found");
					found = true;
					project.AisleNav.AisleNavActivity.SharedLists
							.getNavigableStore().addItems(storeItem);
				}

			}

		}
		if(found) {
			project.AisleNav.AisleNavActivity.SharedLists.getNavigableStore().getStore()
				.setId(project.AisleNav.AisleNavActivity.SharedLists.getCurrentStoreItems().getStore().getId());
		}
		else {
			project.AisleNav.AisleNavActivity.SharedLists.getNavigableStore().clear();
		}
		System.out.println("Done matchItems.");
		System.out.println("Saving...");
		project.AisleNav.AisleNavActivity.caller.save();
		System.out.println("Saving done.");
		return found;
	}
}
